package cn.ds.assets.module.assets.controller.admin.metadataModelBusiness;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataModelBusiness.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelBusiness.BaseMetadataModelBusinessDO;
import cn.ds.assets.module.assets.convert.metadataModelBusiness.BaseMetadataModelBusinessConvert;
import cn.ds.assets.module.assets.service.metadataModelBusiness.BaseMetadataModelBusinessService;

//@Tag(name = "管理后台 - 元数据管理-元模型-业务属性")
//@RestController
//@RequestMapping("/assets/base-metadata-model-business")
@Validated
public class BaseMetadataModelBusinessController {

    @Resource
    private BaseMetadataModelBusinessService baseMetadataModelBusinessService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-元模型-业务属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:create')")
    public CommonResult<Long> createBaseMetadataModelBusiness(@Valid @RequestBody BaseMetadataModelBusinessCreateReqVO createReqVO) {
        return success(baseMetadataModelBusinessService.createBaseMetadataModelBusiness(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-元模型-业务属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:update')")
    public CommonResult<Boolean> updateBaseMetadataModelBusiness(@Valid @RequestBody BaseMetadataModelBusinessUpdateReqVO updateReqVO) {
        baseMetadataModelBusinessService.updateBaseMetadataModelBusiness(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-元模型-业务属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:delete')")
    public CommonResult<Boolean> deleteBaseMetadataModelBusiness(@RequestParam("id") Long id) {
        baseMetadataModelBusinessService.deleteBaseMetadataModelBusiness(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-元模型-业务属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:query')")
    public CommonResult<BaseMetadataModelBusinessRespVO> getBaseMetadataModelBusiness(@RequestParam("id") Long id) {
        BaseMetadataModelBusinessDO baseMetadataModelBusiness = baseMetadataModelBusinessService.getBaseMetadataModelBusiness(id);
        return success(BaseMetadataModelBusinessConvert.INSTANCE.convert(baseMetadataModelBusiness));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-元模型-业务属性列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:query')")
    public CommonResult<List<BaseMetadataModelBusinessRespVO>> getBaseMetadataModelBusinessList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataModelBusinessDO> list = baseMetadataModelBusinessService.getBaseMetadataModelBusinessList(ids);
        return success(BaseMetadataModelBusinessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-元模型-业务属性分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:query')")
    public CommonResult<PageResult<BaseMetadataModelBusinessRespVO>> getBaseMetadataModelBusinessPage(@Valid BaseMetadataModelBusinessPageReqVO pageVO) {
        PageResult<BaseMetadataModelBusinessDO> pageResult = baseMetadataModelBusinessService.getBaseMetadataModelBusinessPage(pageVO);
        return success(BaseMetadataModelBusinessConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-元模型-业务属性 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-business:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataModelBusinessExcel(@Valid BaseMetadataModelBusinessExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataModelBusinessDO> list = baseMetadataModelBusinessService.getBaseMetadataModelBusinessList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataModelBusinessExcelVO> datas = BaseMetadataModelBusinessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-元模型-业务属性.xls", "数据", BaseMetadataModelBusinessExcelVO.class, datas);
    }

}
